import { _decorator, Component, instantiate, Label, Node, Prefab, RichText } from 'cc';
import { Report, SideType } from '../entity/Report';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Utils } from '../common/Utils';
import { ReportPanel } from './ReportPanel';
import { UIHeader } from '../common/UIHeader';
import { EventManager } from '../event/EventManager';
import ReportApi from '../api/ReportApi';
import { ReportFight } from './ReportFight';
import { ReportType } from '../settings/const';
const { ccclass, property } = _decorator;

@ccclass('ReportDetail')
export class ReportDetail extends Component {
    @property(UIHeader)
    uiHeader:UIHeader
    @property(Node)
    fight:Node
    @property(RichText)
    tittle:RichText
    @property(Label)
    time:Label
    @property(Node)
    content:Node
    @property(Prefab)
    reportPanel:Prefab

    report:Report

    start() {
        this.uiHeader.setData('报告详情')
        EventManager.instance.on('报告详情close', this.onClose, this)

        this.fight.on(Node.EventType.TOUCH_END, this.onFight, this)
    }

    setData(report:Report) {
        
        console.log(report)
        this.report = report

        

        if(report.reportType == ReportType.rp_Attack){
            this.fight.active = true
            this.uiHeader.close.active = false
        } else {
            this.fight.active = false
        }

        

        this.tittle.string = report.titleRichText
        this.time.string = Utils.formatDate(new Date(report.startTime * 1000))

        let rp = instantiate(this.reportPanel)
        this.content.addChild(rp)
        rp.getComponent(ReportPanel).setData(report, SideType.Attack)

        rp = instantiate(this.reportPanel)
        this.content.addChild(rp)
        rp.getComponent(ReportPanel).setData(report, SideType.Defense)
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.ReportDetail)
    }

    async onFight(){
        UIManager.instance.showLoading()
        let res = await ReportApi.getBP(parseInt(this.report.battle))

        let ui = await UIManager.instance.openUI(PrefabEnum.ReportFight)
        UIManager.instance.closeLoading
        ui.getComponent(ReportFight).setData(Report.createByObj(res.data))
    }
}


